<?php
/**
mysqli 类
*/
class lib_db_mysqli {
	private $link;
	private $stmt;
	private  $data=array();
	private $config;
	

	public function __construct($config)
	{
		$this->config=$config;
	}
	
	public function connect(){
		$this->link =  mysqli_connect($this->config['db_host'],$this->config['db_username'],$this->config['db_password'],$this->config['db_name']);
		if(mysqli_connect_errno()){
			  printf("Connect failed: %s\n", mysqli_connect_error());
    		exit();
		}
		if(!mysqli_ping($this->link)){			
   			 printf ("Error: %s\n", mysqli_error($link));
   			 exit();
		}

		$this->query('SET NAMES '.$this->config['db_charset']);
		mysqli_set_charset($this->link,$this->config['db_charset']);
	}
	
	public function query($sql){
		$this->stmt = mysqli_query($this->link,$sql);
		if(!$this->stmt) 
			return false;
		return $this->stmt;
	}
	
	public function num_rows($query){
		return @mysqli_num_rows($query);
	}
	
	public function insert_id(){
		return @mysqli_insert_id($this->link);
	}
	
	public function fetchAll($sql,$type='MYSQLI_ASSOC'){
		$this->data = array();
		$getfetch = $type == 'MYSQLI_ASSOC' ? 'mysqli_fetch_assoc' : 'mysqli_fetch_row' ;
		if($this->query($sql)){
			while($row = $getfetch($this->stmt)){
				$this->data[] = $row;
			}
			return $this->data;	
		}else{
			exit('no query');
		}
	}
	
	public function fetch($sql,$type='MYSQLI_ASSOC'){
		$this->data = array();
		$getfetch = $type == 'MYSQLI_ASSOC' ? 'mysqli_fetch_assoc' : 'mysqli_fetch_row' ;
		if($this->query($sql)){
			while($row = $getfetch($this->stmt)){
				$this->data = array_merge($this->data,$row);
			}
			return $this->data;	
		}else{
			exit('no query');
		}
	}
	
	public function update($array,$table,$where){
		global $Config;
		foreach ($array as $k=>$v){
			$ary[]=$k."='".$v."'";
		}
		if(is_array($ary) && sizeof($array)>0){
			$where=isset($where)?' where '.$where:'';
			$sql="update ".$table." set ".implode(',',$ary)." ".$where;
			$Config->debug &&  var_dump($sql);
			return $this->query($sql);
		}
		return false;
	}
	
	public function insert($array,$table){
			global $Config;
			foreach($array as $k => $v){
	        $ary[]=$k."='".$v."'";
	    }
	    $sql="insert into ".$table." set ".implode(',',$ary);
	 	 $Config->debug &&  var_dump($sql);
	    $query=$this->query($sql);
	    return $this->insert_id();
	}
	
		function table_exists($tab){
		$sql="SHOW TABLES LIKE '%".$tab."%'";
		$query=$this->query($sql);
		if($this->num_rows($query)) return true;
			else return false;
	}
	
	function getTableField($table){
		$this->connect();
		$fields = mysqli_list_fields($this->Database,$table,$this->Link_ID);   
		$columns = mysqli_num_fields($fields);   
		for($i=0;$i<$columns;$i++)   {
			$result[]=array('name'=>mysql_field_name($fields,$i),'type'=>mysql_field_type($fields,$i));
		}
		return $result;
	}	
	
  public function field_len($res, $field) {
    return @mysqli_field_len($res, $field);
  }

  public function field_name($res, $field) {
    return @mysqli_field_name($res, $field);
  }

  public function field_type($res, $field) {
    return @mysqli_field_type($res, $field);
  }

  public function list_fields($db, $table) {
    $this->check();
    return @mysqli_list_fields($db, $table, $this->db);
  }

  public function list_tables($db) {
    $this->check();
    return @mysqli_list_tables($db, $this->db);
  }

  public function list_dbs() {
    $this->check();
    return @mysqli_list_dbs($this->db);
  }
	
	
	public function free_result(){
		mysqli_free_result($this->stmt);
	}
	
	public function  getValue($sql){
	    return $this->fetch_value($sql);
	}
		
	public function __destruct(){
		mysqli_close($this->link);
	}
}
?>